<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag,ElRow,ElCol,ElTree,ElForm,ElFormItem,ElInput,ElRadioGroup,ElRadio } from 'element-plus'
import { Table } from '@/components/Table'
import { getTableListApi, delTableListApi } from '@/api/table'
import { useTable } from './web/vCircleTable'
import { TableData } from '@/views/vCircle/table/types.ts'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'
import {getVcirce, updateForum, tree, getxialacaidan} from '@/views/vCircle/table'
import { inject } from "vue";
import Dialog from '@/views/vCircle/web/Dialog.vue'

const { push } = useRouter()
const { register, tableObject, methods } = useTable<TableData>({
  getListApi: getVcirce,
  delListApi: delTableListApi,
  response: {
    list: 'list',
    total: 'total'
  }
})

const { getList, setSearchParams } = methods
getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index'
  },
  {
    field: 'userName',
    label: t('page.bfusername')
  },
  {
    field: 'articleTitle',
    label: t('page.articleTitle')
  },
  {
    field: 'url',
    label: t('page.url'),
    sortable: true
  },
  {
    field: 'createDate',
    label: t('page.createDate'),
    sortable: true
  },
  {
    field: 'userAccount',
    label: t('page.account')
  },
  {
    field: 'articleLike',
    label: t('page.articleLike')
  },
  {
    field: 'articleHits',
    label: t('page.articleHits')
  },
  {
    field: 'articleReviews',
    label: t('page.articleReviews')
  },
  {
    field: 'articleShare',
    label: t('page.articleShare')
  },
  {
    field: 'details',
    label: "状态",
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      if (cellValue==3){
        return '已审核'
      }else if(cellValue==5){
        return '未审核'
      }
    }
  },
  {
    field: 'action',
    width: '260px',
    label: t('tableDemo.action')
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

//局部刷新
const ceshi = inject("reload");

//详情查询
const action = (row: TableData, type: string) => {
  push(`/external-link/lookVcircle3—${type}?microcircleId=${row.microcircleId}`)
}

//表单查询
import { Form } from '@/components/Form'
import {FormSchema} from "@/types/form";
const schema = reactive<FormSchema[]>([
  {
    field: 'title',
    label: '文章名称',
    component: 'Input'
  },
  {
    field: 'name',
    label: '用户名称',
    component: 'Input'
  }
  ,{
    field: 'acc',
    label: '用户账号',
    component: 'Input'
  }
])
//审核
const zzzz = ref(false)

let form1 = ref()
const dialog=(row)=>{
  zzzz.value=true
  form1=row
}
const form = reactive({
  id: '',
  detaId: "3",
  desc: '',
})

const formSubmit = (res) => {
  form.id=res.microcircleId
  zzzz.value=false
  updateForum(form).then((res)=>{})
  getList();
}
</script>

<template>
      <ContentWrap>
        <Search
            :expand = "true"
            :schema="schema"
            @search= "setSearchParams"
            @reset= "setSearchParams"
        />
        <Table
            v-model:pageSize="tableObject.pageSize"
            v-model:currentPage="tableObject.currentPage"
            :columns="allSchemas.tableColumns"
            :data="tableObject.tableList.slice((tableObject.currentPage-1) * tableObject.pageSize, tableObject.currentPage * tableObject.pageSize)"
            :loading="tableObject.loading"
            :pagination="{
        total: tableObject.total
      }"
            @register="register"
        >
          <template #action="{ row }">
            <ElButton type="success" @click="action(row, 'detail')" v-if="row.details==3">
              {{ t('详情') }}
            </ElButton>
            <ElButton type="danger" @click="dialog(row)" v-else>
              {{ t('审核') }}
            </ElButton>
          </template>
        </Table>
        <Dialog v-model="zzzz" :title="t('dialogDemo.dialog')">
          <ElForm :model="form" label-width="120px">
            <ElFormItem label="课程名称">
              {{form1.articleTitle}}
            </ElFormItem>
            <ElFormItem label="审核结果">
              <ElRadioGroup v-model="form.detaId">
                <ElRadio label="3">审核通过</ElRadio>
                <ElRadio label="6">审核不通过</ElRadio>
              </ElRadioGroup>
            </ElFormItem>

            <ElFormItem label="备注">
              <ElInput v-model="form.desc" type="textarea" />
            </ElFormItem>
          </ElForm>
          <template #footer>
            <ElButton type="primary" @click="formSubmit(form1)">{{ t('dialogDemo.submit') }}</ElButton>
            <ElButton @click="zzzz = false">{{ t('dialogDemo.close') }}</ElButton>
          </template>
        </Dialog>
      </ContentWrap>
</template>
